﻿using Furion.DependencyInjection;
using Furion.DynamicApiController;
using HYHY_Core.Enums;
using HYQY_App.Admin_System.Interface;
using HYQY_App.Applet.Interface;
using HYQY_Core.Entities.Ware;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HYQY_App.Admin_System.Dtos.CommodityAdminDto;
using static HYQY_App.Applet.Dtos.ClassifyDto;

namespace HYQY_App.Applet.Service
{
    /// <summary>
    /// 小程序Home首页
    /// </summary>
    [ApiDescriptionSettings(ApiGroupEnumsCode.AppletClassify, Name = "AppletClassify")]
    public class ClassifyService : IDynamicApiController, ITransient, IClassifyService
    {
        /// <summary>
        /// sql
        /// </summary>
        private readonly ISqlSugarClient _db;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public ClassifyService(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 获取分类类目
        /// </summary>
        /// <returns></returns>
        public async Task<List<AppletSelectCategoryListOutPut>> AppletSelectCategoryList()
        {
            var list = await _db.Queryable<Category>()
                .Select(it => new AppletSelectCategoryListOutPut
                {
                    Id = it.Id,
                    Name = it.Name,
                    Sort = it.Sort
                })
                .OrderBy(it => it.Sort)
                .ToListAsync();

            return list;
        }

        /// <summary>
        /// 获取分类页面商品信息
        /// </summary>
        /// <returns></returns>
        public async Task<AppletSelectWareListResp> AppletSelectWareList()
        {
            RefAsync<int> total = 0;


            var wareInfos = await _db.Queryable<WareBank>()
                .Where((t1) => t1.IsClassify == true)
                .Select((t1) => new AppletWareInfos
                {
                    Price = t1.Price,
                    WareName = t1.WareName,
                    WareCode = t1.WareCode,
                    CreateMan = t1.CreateMan,
                    UpdateMan = t1.UpdateMan,
                    WareDetail = t1.WareDetail,
                    CategoryId = t1.CategoryId,
                    CreateTime = t1.CreateTime,
                    UpdateTime = t1.UpdateTime,
                    IsClassify = t1.IsClassify,
                    Stock = t1.Stock,
                    SalesVolume = t1.SalesVolume,
                    IsGuessWhatYouLike = t1.IsGuessWhatYouLike,
                }).ToListAsync();

            var list = await _db.Queryable<WareBank>()
                .LeftJoin<Category>((t1, t2) => t1.CategoryId == t2.Id)
               .Where((t1) => t1.IsClassify == true)
               .Select((t1, t2) => new AppletSelectWareListOutPut
               {
                   CategoryId = t2.Id,
                   Name = t2.Name,

               })
               .MergeTable()
               .Distinct()
               .ToListAsync();
            total = list.Count;

            foreach (var item in list)
            {
                item.WareInfos = wareInfos.Where(it => it.CategoryId == item.CategoryId).ToList();
            }

            var imges = await _db.Queryable<Images>()
                .Where(it => wareInfos.Select(t => t.WareCode).Contains(it.Code)).ToListAsync();

            var categorys = await _db.Queryable<Category>()
                .Where(it => wareInfos.Select(t => t.CategoryId).Contains(it.Id)).ToListAsync();

            foreach (var item in wareInfos)
            {
                item.Path = imges.FirstOrDefault(it => it.Code == item.WareCode)?.Url ?? string.Empty;
                item.CategoryName = categorys
                    .FirstOrDefault(it => it.Id == item.CategoryId)?.Name ?? string.Empty;
            }

            return new AppletSelectWareListResp
            {
                Total = total,
                List = list,
                StatusCode = 200,
                Message = "调用成功"
            };
        }
    }
}
